//
//  InstallLayerPwrd.h
//  client
//
//  Created by galileoliu on 1/7/2019.
//
//

#ifndef InstallLayerPwrd_h
#define InstallLayerPwrd_h

#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "InstallLayer.h"
#ifdef USE_PWRD
#include "UpdateSpeedCounter.hpp"
#include "Pwrd/PwrdInfoString.hpp"
#endif

USING_NS_CC;
using namespace cocos2d::extension;

class InstallLayerPwrd : public InstallLayer
{
public:
    CREATE_FUNC(InstallLayerPwrd);

    InstallLayerPwrd();

    virtual ~InstallLayerPwrd();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

#ifdef USE_PWRD
    virtual void update(float);
    PwrdInfoString& getPwrdInfoString();
private:
    void initAssetsManager();
	void startUpdate(Ref* sender);
    void onProgress(AssetsManagerEx::State curVersionStage, float percent, int64_t totalSize, int64_t nowSize);
    void onError(AssetsManagerEx::State curVersionStage, EventAssetsManagerEx::EventCode errorCode, EventAssetsManagerEx* event = nullptr);
    void onUpdateSuccess();
    void continueUpdate();
    void stopUpdate();
    void clear();
    void setResourceVersion(const std::string& version);
    
    void _requestVersionInfo();
    void _onVersionInfoResponded(network::HttpClient *sender, network::HttpResponse *response);
  
    void _onGetVersionInfoSucceeded();
    void _onGetVersionInfoFailed();
    
    std::string getDownSizeShowString(int64_t totalSize, int64_t nowSize);
    
    std::vector<std::string> _cdnUrlList;
    std::string _cdnUrl;
    int _cdnUrlIndex;
    
    bool _waitMessageBox;
    bool _shouldStop;
    bool _flagShowPatchSizeDialog;
    
    EventAssetsManagerEx::EventCode _lastErrorCode;
    AssetsManagerEx::State _curStage;
    
    UpdateSpeedCounter _speedCounter;
    PwrdInfoString _pwrdInfoString;
    PwrdErrorString _pwrdErrorString;
    
#endif
    virtual void goStart();
    virtual void goExit();
    virtual void goNext();
    
private:
    std::string _newResourceVersion;
    
    AssetsManagerEx* _am;
    EventListenerAssetsManagerEx* _amListener;
    double _downLoadTime;
};

#endif
